// 版权归陈超所有


#include "UI/WidgetController/CC_AttributeMenuWidgetController.h"

#include "CC_GameplayTags.h"
#include "AbilitySystem/CC_AttributeSet.h"
#include "AbilitySystem/Data/DataAsset_AttributeInfo.h"

void UCC_AttributeMenuWidgetController::BroadcastInitialValues()
{
	if (CC_AttributeSet == nullptr)
	{
		CC_AttributeSet = CastChecked<UCC_AttributeSet>(AttributeSet);
	}
	checkf(AttributeInfo, TEXT("在[%s]中没有配置AttributeInfo"), *GetNameSafe(this));

	for (auto& Pair : CC_AttributeSet->TagsToAttributes)
	{
		BroadcastAttributeInfo(Pair.Key, Pair.Value());
	}
}

void UCC_AttributeMenuWidgetController::BindCallbacksToDependencies()
{
	//属性发生变化时,希望也进行广播
	if (CC_AttributeSet == nullptr)
	{
		CC_AttributeSet = CastChecked<UCC_AttributeSet>(AttributeSet);
	}
	checkf(AttributeInfo, TEXT("在[%s]中没有配置AttributeInfo"), *GetNameSafe(this));

	for (auto& Pair : CC_AttributeSet->TagsToAttributes)
	{
		AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(Pair.Value()).AddLambda(
			[this, Pair](const FOnAttributeChangeData& Data)
			{
				BroadcastAttributeInfo(Pair.Key, Pair.Value());
			}
		);
	}
	
}

void UCC_AttributeMenuWidgetController::BroadcastAttributeInfo(const FGameplayTag& Tag,
	const FGameplayAttribute& Attribute)
{
	FAttributeInfo Info = AttributeInfo->FindAttributeInfoFromTag(Tag);
	Info.AttributeValue = Attribute.GetNumericValue(CC_AttributeSet);
	OnAttributeInfoSignature.Broadcast(Info);
}
